﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BKH.BLL;



namespace BKH.MyUserControl.UCHienThi
{
    public partial class ucQuanLyKhoa : UserControl
    {
        public ucQuanLyKhoa()
        {
            InitializeComponent();
            ShowData();
            btnCapNhat.Visible = false;
        }
        private String ID_Co;

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            InsertKhoa();

        }
        void InsertKhoa()
        {
            KhoaBUO obj = new KhoaBUO();
            tbl_Khoa objKhoa = new tbl_Khoa();
            
            objKhoa.WardNumber = txtMaKhoa.Text;
            objKhoa.WardName = txtKhoa.Text;
            objKhoa.Location = txtAddress.Text;
            objKhoa.Tel = txtTel.Text;

            if (obj.Insert(objKhoa))
            {
                MessageBox.Show("Them thanh cong");
            }
            else
            {
                MessageBox.Show("Them that bai");
            }

            ShowData();
 
        }
        
        void ShowData()
        {
            KhoaBUO obj = new KhoaBUO ();
            dataGridView1.AutoGenerateColumns = false;
            
            dataGridView1.DataSource = obj.GetAll();
            


            }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {

            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                // Only RowHeader and Cells (which make up a row) should fire a menu
                if (hit.Type == DataGridViewHitTestType.RowHeader
                    || hit.Type == DataGridViewHitTestType.Cell
                    && hit.RowIndex >= 0)
                {
                    // Notice how I assign the menu to the dataGridView below...

                    dataGridView1.ClearSelection();
                    dataGridView1.Rows[hit.RowIndex].Selected = true;
                    dataGridView1.ContextMenuStrip = contextMenuStrip1;
                    dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));


                    ID_Co=dataGridView1.Rows[hit.RowIndex].Cells[3].Value.ToString();

                }

                // and then I remove the menu here...
                // This is because after we assign it to the dataGridView
                // it allows you to right-click anywhere on the control to
                // get a menu.  Try removing this line and expirement.
                dataGridView1.ContextMenuStrip = null; 

            }

        }

        private void xoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            KhoaBUO obj = new KhoaBUO();
            DialogResult ds = MessageBox.Show("Co nen xoa hay khong", "Xoa du lieu", MessageBoxButtons.YesNo);
            if (ds == DialogResult.Yes)
            {
                if (obj.Delete(int.Parse(ID_Co)))
                {
                    MessageBox.Show("Xoa thanh cong");
                }
                else
                {
                    MessageBox.Show("Xoa that bai");
                }
            }
            else
            {

            }

            ShowData();
        }

        private void sToolStripMenuItem_Click(object sender, EventArgs e)
        {
            KhoaBUO obj = new KhoaBUO();
            tbl_Khoa khoa = obj.GetSingle(int.Parse(ID_Co));
            txtKhoa.Text = khoa.WardName;
            txtMaKhoa.Text = khoa.WardNumber;
            txtAddress.Text = khoa.Location;
            txtTel.Text = khoa.Tel;
            btnCapNhat.Visible = true;    
      
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            KhoaBUO obj = new KhoaBUO();
            tbl_Khoa khoa = obj.GetSingle(int.Parse(ID_Co));
            // Update
            khoa.WardName = txtKhoa.Text;
            khoa.WardNumber = txtMaKhoa.Text;
            khoa.Location = txtAddress.Text;
            khoa.Tel = txtTel.Text;

            if (obj.Update(int.Parse(ID_Co), khoa))
            {
                MessageBox.Show("Thành Công !");

            }
            else
            {
                MessageBox.Show("Thất bại");
            }

            ShowData();
            btnCapNhat.Visible = false;
        }
    
    }
}
